package com.ParkingManager.app.web;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ParkingManager.app.bean.User;
import com.ParkingManager.app.carPlateRecognition.carPlateRecognition;
import com.ParkingManager.app.service.UserService;
import com.ParkingManager.app.util.PathManager;

@RestController
public class UserController {
	@Autowired
	private UserService service;
	@Autowired
	private HttpSession session;
	
	
	@RequestMapping("User/getUser")
	public List<HashMap> getUser(String id) {
		System.out.print("----------------------------" + id);
		return service.getUser();
	}
	
	@RequestMapping("User/carPlateRecognition")
	public String[] carPlateRecognition()
	{
		return carPlateRecognition.carPlateRec(PathManager.imagePath +File.separator+ "1.jpg");
	}
	// 登录
	@RequestMapping("user/login")
	public int login(User user) {
		
		int flag=service.login(user);
		if(flag==1){
			// 登录成功，将信息添加到session里头
			session.setAttribute("user", user);
		}
		return flag;
	}
	
	//添加
	@RequestMapping("User/insertUser")
	public boolean insertUser(String userName,String password,int userType, String parkingId) {
		return service.insertUser(userName, password, userType, parkingId);
	}
	
	//更新
	@RequestMapping("User/updateUser")
	public boolean updateUser(String userName, String parkingId) {
		return service.updateUser(userName,  parkingId);
	}
	
	//删除
	@RequestMapping("User/deleteUser")
	public boolean deleteUser(String userName) {
		return service.deleteUser(userName);
	}
	
	//统计
	int getCountUser() {
		return service.getCountUser();
	}
	
	//删除
	@RequestMapping("User/searchUser")
	public List<HashMap<String, Object>> searchUser(String str) {
		//先按用户名搜索
		System.out.print(str);
		if(str.isEmpty()){
			str="场";
		}
		String str1=str;
		List<HashMap<String, Object>> result = service.searchByUser(str); 
		if (!result.isEmpty()) {
			System.out.print("按用户名查找");
		}
		
		List<HashMap<String, Object>> result2= service.searchByPname(str1);
		
		boolean flag=true;
		for (int i = 0; i < str1.length(); i++){
			System.out.println(str1.charAt(i));
			if (!Character.isDigit(str1.charAt(i))){
				flag=false;
			}
		}
		if(flag){
			int id=Integer.parseInt(str1);
			List<HashMap<String, Object>> result3= service.searchByPid(id);
			if (!result3.isEmpty()) {
				// 如果按停车场id搜索有结果
				System.out.print("按停车场id查找");
				for(HashMap<String, Object> m : result3) {//遍历result3把数据添加进result中

					result.add(m);//将合并后的一条数据添加到query集合中
				}
			}
		}
		if (!result2.isEmpty()) {
			// 如果按停车场名搜索有结果
			System.out.print("按停车场名查找");
			for(HashMap<String, Object> m : result2) {//遍历result2把数据添加进result中
			     
			    result.add(m);//将合并后的一条数据添加到query集合中
			}
		}
		
	
		return result;	//返回拼接结果
	}

}
